package com.qm.network;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author wb
 * @date 2023-06-16 14:53
 * @description:
 */
public class MyUrl {
    public static void main(String[] args) throws IOException {
        /**
         * 通过URL下载QQ到D盘
         */
        URL url = new URL("https://down.qq.com/qqweb/PCQQ/PCQQ_EXE/PCQQ2021.exe");
        System.out.println(url.getProtocol());// 获取URL的协议
        System.out.println(url.getHost());// 获取URL的主机
        System.out.println(url.getPath());// 返回URL路径部分。

        // 建立连接
        URLConnection urlConnection = url.openConnection();
        // 获取到资源
        InputStream inputStream = urlConnection.getInputStream();
        // 下载到D盘(边读边写 下载到d盘)
        OutputStream outputStream = new FileOutputStream("d:/QQ.exe");
        byte[] bytes = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }

    }
}
